'use strict';
module.exports = {
	/**
	 * 更新O制片方收益展示变更配置
	 * @url admin/pages/kh/zpf/ladder/update 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			_id,
			tv_id,
			ladder_cut
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------

		// 获取剧集数据
		let tvData;
		let tv_name;
		if (tv_id) {
			const tvCandidate = await db.collection("a_tv").doc(tv_id).get()
			console.log(tvCandidate)
			if (tvCandidate.data.length == 0) {
				res.code = 1;
				res.msg = "获取剧集定义失败"
				return res;
			}
			tvData = tvCandidate.data[0];
			tv_name = tvData.tv_name;
		}else {
			tv_name = null;
		}


		const dbName = "a_ladder_config"
		const oldDbData = await vk.baseDao.findById({
			dbName: dbName,
			id: _id
		})

		if (!oldDbData) {
			res.code = 1;
			res.msg = "获取原始配置失败";
			return res;
		}

		// 进行更新
		await vk.baseDao.updateById({
			dbName: dbName,
			id: _id,
			dataJson: {
				tv_id,
				tv_name,
				ladder_cut
			}
		})
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}